package jersey;

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.config.ClientConfig;
import com.sun.jersey.api.client.config.DefaultClientConfig;
import com.sun.jersey.api.json.JSONConfiguration;
import com.sun.jersey.multipart.FormDataBodyPart;
import com.sun.jersey.multipart.FormDataMultiPart;
import com.sun.jersey.multipart.file.FileDataBodyPart;
import com.sun.jersey.multipart.impl.MultiPartWriter;
import dto.ResultDto;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import util.PropertiesUtils;

import javax.ws.rs.core.MediaType;
import java.io.*;
import java.util.UUID;

public class UploadFileUtils {

private final static Logger logger = LogManager.getLogger(UploadFileUtils.class);

    public static ResultDto upload(InputStream inputStream,String key)  {
        ResultDto resultDto = null;
        OutputStream outputStream = null;
        String uuid = UUID.randomUUID().toString();
        //创建临时文件
        File file = null;
        Client writerClient = null;
        try {
            byte[] buf = new byte[inputStream.available()];
            inputStream.read(buf);
            file = File.createTempFile(uuid,".jpg");
            outputStream = new FileOutputStream(file);
            outputStream.write(buf);
            //创建multipart表单
            FormDataMultiPart part = new FormDataMultiPart();
            part.bodyPart(new FileDataBodyPart("img",file));
            part.bodyPart(new FormDataBodyPart("key", key));
            //创建jersey client
            ClientConfig cc = new DefaultClientConfig();
            cc.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE);
            cc.getClasses().add(MultiPartWriter.class);

            writerClient = Client.create(cc);
            writerClient.setConnectTimeout(3000);
            writerClient.setReadTimeout(3000);
            WebResource resource = writerClient.resource(PropertiesUtils.getProperties("image.server"));
            ClientResponse response =resource.accept(MediaType.APPLICATION_JSON).type(MediaType.MULTIPART_FORM_DATA).post(ClientResponse.class,part);
            resultDto = response.getEntity(ResultDto.class);
            return resultDto;
        } catch (IOException e){
            logger.error(e);
            return  resultDto;
        }   finally{
            if (writerClient!=null)
                writerClient.destroy();
            try {
                if (inputStream!=null)
                    inputStream.close();
                if (outputStream!=null)
                    outputStream.close();
            } catch (IOException e) {
                logger.error(e);
            }
            if (file!=null){
               file.delete();
            }
        }

    }

    public static void main(String[] args) throws IOException {
        InputStream inputStream = new FileInputStream("C:\\Users\\YEAL4\\Downloads\\eg_tulip.jpg");
        ResultDto resultDto = UploadFileUtils.upload(inputStream,"test/allen1.jpg");
        System.out.println(resultDto);
    }
}
